package lottery;

import javafx.embed.swing.JFXPanel;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.ArrayList;
import java.util.Random;

/**
 * Created by huanggc on 2019/10/29.
 */
public class Choujiang {

    static ArrayList<String> users = new ArrayList<>();
    static Random r = new Random();
    static boolean flag = true;

    public static void main(String[] args) {

        initUsers();

        JFrame frame = new JFrame("惊喜大抽奖");
        JPanel mainPanel = new JPanel();
        //mainPanel.setBorder(new LineBorder(Color.RED,5));
        frame.setContentPane(mainPanel);

        Font font = new Font("楷体",Font.BOLD,30);
        Font btnFont = new Font("圆体",Font.BOLD,25);
        JPanel choujiangPanel = new JPanel();
        choujiangPanel.setBounds(80,120,300,50);
        JTextField jtf = new JTextField(10);
        jtf.setFont(font);
        choujiangPanel.add(jtf);
        JButton btn = new JButton("开始");
        btn.setFont(btnFont);
        //choujiangPanel.setBorder(new LineBorder(Color.RED,5));
        choujiangPanel.add(btn);
        mainPanel.add(choujiangPanel);
        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String text = btn.getText();//获取按钮上面的文字
                if(text.equals("开始")){
                    btn.setText("停止");
                    flag = true;
                    new Thread(){
                        @Override
                        public void run() {
                            System.out.println("开始抽奖，人员数：" + users.size());
                            String luckyBoy = "";
                            while(flag){
                                luckyBoy = users.get(r.nextInt(users.size()));
                                jtf.setText(luckyBoy);
                                try {
                                    Thread.sleep(50);
                                } catch (InterruptedException e1) {
                                    e1.printStackTrace();
                                }
                            }

                            int sleepTime = 50;
                            while(sleepTime < 1000){
                                luckyBoy = users.get(r.nextInt(users.size()));
                                jtf.setText(luckyBoy);
                                try {
                                    sleepTime = sleepTime + 200;
                                    Thread.sleep(sleepTime);
                                } catch (InterruptedException e1) {
                                    e1.printStackTrace();
                                }
                            }
                            users.remove(luckyBoy);
                        }
                    }.start();
                }else{
                    btn.setText("开始");
                    flag = false;
                }

            }
        });

        mainPanel.setLayout(null);
        frame.setLayout(null);//不使用系统布局
        frame.setBounds(500,300,500,400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//点关闭退出程序
        frame.setVisible(true);
    }

    /**
     * 初始化抽奖人员列表
     */
    private static void initUsers() {
        File file = new File("C:\\Users\\huanggc\\Desktop\\课堂积分\\1.txt");
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));
            String str = "";
            while((str = br.readLine()) != null){
                users.add(str.split("#")[0]);
            }
            br.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
